package com.ly.hotel.view.fragments.youhui;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Bundle;
import android.util.TypedValue;
import android.view.View;
import android.widget.Button;

import androidx.viewpager.widget.ViewPager;

import com.ly.hotel.R;
import com.ly.hotel.model.YouhuiMessage;
import com.ly.hotel.util.LanguageUtil;

import net.lucode.hackware.magicindicator.MagicIndicator;
import net.lucode.hackware.magicindicator.ViewPagerHelper;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.CommonNavigator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.CommonNavigatorAdapter;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerTitleView;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.indicators.WrapPagerIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.titles.SimplePagerTitleView;

import java.util.ArrayList;
import java.util.List;


/**
 * Created by SHUBEN on 2018/1/26.
 */

public class YouhuiDialog extends Dialog {

    private Context context;
    private List<YouhuiMessage> msgList = new ArrayList<YouhuiMessage>();

    private ViewPager mViewPager;

    public YouhuiDialog(Context context, int theme) {
        super(context, theme);
        this.context = context;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_youhui);
        Button backBtn = findViewById(R.id.dialog_back).findViewById(R.id.dialog_back);
        backBtn.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                dismiss();
            }
        });
        mViewPager =  findViewById(R.id.view_pager);
    }

    public void InitView(List<YouhuiMessage> s) {
        msgList = s;
        YouhuiAdapter mExamplePagerAdapter = new YouhuiAdapter(msgList);
        mViewPager.setAdapter(mExamplePagerAdapter);
        initMagicIndicator();
    }

    private void initMagicIndicator() {
        MagicIndicator magicIndicator = (MagicIndicator) findViewById(R.id.magic_indicator);
//        magicIndicator.setBackgroundColor(Color.parseColor("#cfddee"));
        CommonNavigator commonNavigator = new CommonNavigator(context);
        commonNavigator.setScrollPivotX(0.35f);
        commonNavigator.setAdapter(new CommonNavigatorAdapter() {
            @Override
            public int getCount() {
                return msgList == null ? 0 : msgList.size();
            }

            @Override
            public IPagerTitleView getTitleView(Context context, final int index) {
                SimplePagerTitleView simplePagerTitleView = new SimplePagerTitleView(context);
                simplePagerTitleView.setText(LanguageUtil.getTextFromLocalLanguage(msgList.get(index).getTitle()));
                simplePagerTitleView.setNormalColor(Color.parseColor("#dddddddd"));
                simplePagerTitleView.setSelectedColor(Color.parseColor("#ffffff"));
                simplePagerTitleView.setTextSize(TypedValue.COMPLEX_UNIT_PX,context.getResources().getDimensionPixelSize(R.dimen.px33));
                simplePagerTitleView.setTypeface(Typeface.DEFAULT_BOLD);
                simplePagerTitleView.setFocusable(true);
                simplePagerTitleView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        v.requestFocus();
                        v.requestFocusFromTouch();
                    }
                });
                simplePagerTitleView.setOnFocusChangeListener(new View.OnFocusChangeListener() {
                    @Override
                    public void onFocusChange(View view, boolean b) {
                        mViewPager.setCurrentItem(index);
                    }
                });
                return simplePagerTitleView;
            }

            @Override
            public IPagerIndicator getIndicator(Context context) {
                WrapPagerIndicator indicator = new WrapPagerIndicator(context);
                indicator.setFillColor(Color.parseColor("#0382C8"));
                return indicator;
            }
        });
        magicIndicator.setNavigator(commonNavigator);
        ViewPagerHelper.bind(magicIndicator, mViewPager);
    }
}